package cn.edu.cqvie.ssm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.cqvie.ssm.entity.Page;
import cn.edu.cqvie.ssm.entity.Student;
import cn.edu.cqvie.ssm.model.StudentModel;
import cn.edu.cqvie.ssm.model.impl.StudentModelImpl;

/**
 * 分页查询学生信息
 * @author zhengsh
 */
@WebServlet("/page")
public class PageServlet extends HttpServlet {
	
	StudentModel sm = new StudentModelImpl();
	private static final long serialVersionUID = 1L;
       

    public PageServlet() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		list(request, response);		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	/**
	 * 返回列表页面
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		int num = req.getParameter("num") == null ? 1: Integer.valueOf(String.valueOf(req.getParameter("num")));
		int size = 2;
		int rowCount = sm.countAllStudents();
		Page page = new Page(num, size, rowCount);
		List<Student> stus = sm.list(page.getStartRow(), size);
		req.setAttribute("page", page);
		req.setAttribute("stus", stus);
		req.getRequestDispatcher("8_2/list.jsp").forward(req, resp);
	}


}
